package com.fr.football;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.lvmama.search.spider.proxy.ProxyIp;
import com.lvmama.search.spider.proxy.ProxyManager;

@SuppressWarnings("deprecation")
public class RemoteData {
	private static final String FILEPATH = "F:\\data\\source1.txt";
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	public static void main(String[] args) throws ParseException, IOException {
		// 创建Httpclient对象  

        FileWriter fr = new FileWriter(FILEPATH);
        
        Date begin = dateFormat.parse("2013-01-11");
        Date end = dateFormat.parse("2018-05-20");
        // 创建http GET请求  
        Map<String, String> params = new HashMap<>();
        params.put("type", "jc");
        params.put("companys", "1,3,12,4,8,9");
        params.put("issue", "2018-05-10");
        ProxyManager.init();
        FileWriter ips = new FileWriter("F:\\data\\ip.txt");
        ips.append(JSON.toJSONString(ProxyManager.getProxyList()));
        ips.close();
        while (begin.before(end)) {
        	String now = dateFormat.format(begin);
        	System.out.println(now);
        	params.put("date", now);
        	ProxyIp ip = ProxyManager.getProxyIp();
        	HttpHost host = null;
        	if(ip != null) {
        		host = new HttpHost(ip.getIp(), ip.getPort(), ip.getScheme());
   		 	}else {
   		 		continue;
   		 	}
        	RequestConfig requestConfig = RequestConfig.custom()  
                    .setConnectionRequestTimeout(5000)   //从连接池中获取连接的超时时间  
                            //与服务器连接超时时间：httpclient会创建一个异步线程用以创建socket连接，此处设置该socket的连接超时时间  
                    .setConnectTimeout(5000)  
                    .setSocketTimeout(5000)               //socket读数据超时时间：从服务器获取响应数据的超时时间  
                    .setProxy(host)
                    .build();  
   	     	CloseableHttpClient httpClient = HttpClientBuilder.create()  
                    .setDefaultRequestConfig(requestConfig)  
                    .build(); 
			try {
				HttpPost httpPost = new HttpPost("http://odds.zgzcw.com/odds/oyzs_ajax.action");
	            List<NameValuePair> formparams = setHttpParams(params);
	            UrlEncodedFormEntity param;
				param = new UrlEncodedFormEntity(formparams, "UTF-8");
				//通过setEntity()设置参数给post
	            httpPost.setEntity(param);
	            //利用httpClient的execute()方法发送请求并且获取返回参数
	            HttpResponse response = httpClient.execute(httpPost);
	            String httpEntityContent = getHttpEntityContent(response);
	            if (StringUtils.isNotBlank(httpEntityContent)) {
	            	@SuppressWarnings("unchecked")
					List<FootballInfo> list = JSON.parseObject(httpEntityContent, new TypeReference<List<FootballInfo>>(){}); 
		            if (CollectionUtils.isNotEmpty(list)) {
		            	System.out.println(now + "getListSize:" + list.size());
		            	for(FootballInfo fi : list) {
		            		fr.append(JSON.toJSONString(fi) + "\n");
		            	}
		            }
	            }
			} catch (Exception e) {
				System.err.println(e);
			}
//			try {
//				Thread.sleep(10);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
            begin.setDate(begin.getDate() + 1);
        }
        fr.close();
	}

	   private static List<NameValuePair> setHttpParams(Map<String, String> paramMap) {
	        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
	        Set<Map.Entry<String, String>> set = paramMap.entrySet();
	        for (Map.Entry<String, String> entry : set) {
	            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
	        }
	        return formparams;
	    }
	   
	    private static String getHttpEntityContent(HttpResponse response) throws IOException, UnsupportedEncodingException {
	        //通过HttpResponse 的getEntity()方法获取返回信息
	         HttpEntity entity = response.getEntity();
	         if (entity != null) {
	             InputStream is = entity.getContent();
	             BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	             String line = br.readLine();
	             StringBuilder sb = new StringBuilder();
	             while (line != null) {
	                 sb.append(line + "\n");
	                 line = br.readLine();
	             }
	             br.close();
	             is.close();
	             return sb.toString();
	         }
	         return "";
	     }
}
